//
//  StatusToolBar.swift
//  SinaWeibo
//
//  Created by 郭平 on 15/10/19.
//  Copyright © 2015年 郭平. All rights reserved.
//

import UIKit

private let RELAY_NUM =  10000

class StatusToolBar: UIView {

    /// 显示转发/评论/赞/的数据
    var status: Status? {
        didSet{
            setStatus()
        }
    }
    
    /// 三个按钮的数据
    private lazy var buttons: [UIButton] = [UIButton]()
    /// 两条分割线
    private lazy var splites: [UIImageView] = [UIImageView]()
    
    var retweetButton: UIButton?    // 转发
    var commentButton: UIButton?    // 评论
    var attitudButton: UIButton?    // 赞
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        retweetButton = addChildButton("timeline_icon_retweet", defatuleTitle: "转发")
        commentButton = addChildButton("timeline_icon_comment", defatuleTitle: "评论")
        attitudButton = addChildButton("timeline_icon_unlike", defatuleTitle: "赞")
        
        addSpliteView()
        addSpliteView()
    }
    
    /**
    设置值
    */
    private func setStatus() {
        
        if let st = status {
            
            setCount(retweetButton!, count: st.reposts_count, defalutTitle: "转发")
            setCount(commentButton!, count: st.comments_count, defalutTitle: "评论")
            setCount(attitudButton!, count: st.attitudes_count, defalutTitle: "赞")
        }
    }
    
    /**
    设置每个按钮身上的数值
    
    - parameter button:       <#button description#>
    - parameter count:        <#count description#>
    - parameter defalutTitle: <#defalutTitle description#>
    */
    private func setCount(button: UIButton,count: Int,defalutTitle: String) {
        if count != 0 {
            if count < RELAY_NUM {
            
                button.setTitle("\(count)", forState: UIControlState.Normal)
            }else {
            
                let result = count / 1000
                var resultStr = "\(CGFloat(result) / CGFloat(10)) 万"
                
                resultStr = resultStr.stringByReplacingOccurrencesOfString(".0", withString: "")
                button.setTitle(resultStr, forState: UIControlState.Normal)
            }
        }else{
            
            button.setTitle(defalutTitle, forState: UIControlState.Normal)
        }
    }

    /**
    添加分割线
    */
    private func addSpliteView() {
        
        let imageView = UIImageView(image: UIImage(named: "timeline_card_bottom_line"))
        
        addSubview(imageView)
        
        splites.append(imageView)
    }
    
    /**
    添加按钮
    
    - parameter imageName:     图片名字
    - parameter defatuleTitle: 默认标题
    
    - returns: 返回一个初始化的按钮
    */
    private func addChildButton(imageName: String, defatuleTitle: String) -> UIButton {
        
        let button = UIButton()
        
        button.setImage(UIImage(named: imageName), forState: UIControlState.Normal)
        
        button.setBackgroundImage(UIImage(named: "timeline_card_bottom_background"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "timeline_card_bottom_background_highlighted"), forState: UIControlState.Highlighted)
        button.setTitle(defatuleTitle, forState: UIControlState.Normal)
        button.setTitleColor(RGB(r: 80, g: 80, b: 80), forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        button.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 5)
        
        addSubview(button)
        
        buttons.append(button)      // 把按钮添加到数组里
        
        return button
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let childViewWidth = SCREENW / CGFloat(buttons.count)
        
        for i in 0..<buttons.count {
            
            let childView = buttons[i]
            
            childView.size = CGSizeMake(childViewWidth, height)
            childView.x = CGFloat(i) * childView.width
        }
        
        for i in 0..<splites.count {
            
            let childView = splites[i]
            childView.centerX = childViewWidth * CGFloat(i + 1)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}
